/**
 * @description 登录、获取用户信息、退出登录、清除token逻辑，不建议修改
 */
import { useSettingsStore } from './settings'
import { useTabsStore } from './tabs'
import  AccountApi from '/@/api/AccountApi'
import { tokenName } from '/@/config'
import { getToken, removeToken, setToken } from '/@/utils/token'
import { isArray, isString } from '/@/utils/validate'
import { gp } from '/@vab/plugins/vab'
import { defineStore } from 'pinia';
import {UserTaskModel} from "@logicflow/extension";
import {useRoutesStore} from "/@/store/modules/routes.ts";
import {getLocalStorageCache, removeLocalStorageCache} from "/@/utils/localStorage.ts";
import router from "/@/router";

const appPrefix = 'advisory_company_'
export const useUserStore = defineStore('user', {
  persist: {
    enabled:true,
    strategies: [
      {
        key: appPrefix+'userInfo',
        storage: window.localStorage
      }
    ]
  },
  state: (): UserModuleType => ({
    token: getToken() as string,
    username: '',
    avatar: './static/svg/avatar.svg',
  }),
  getters: {
    getToken: (state) => state.token,
    getUsername: (state) => state.username,
    getAvatar: (state) => state.avatar,
  },
  actions: {
    /**
     * @description 设置token
     * @param {*} token
     */
    setToken(token: string) {
      this.token = token
      setToken(token)
    },
    /**
     * @description 设置用户名
     * @param {*} username
     */
    setUsername(username: string) {
      this.username = username
    },
    /**
     * @description 设置头像
     * @param {*} avatar
     */
    setAvatar(avatar: string) {
      this.avatar = avatar
    },
    /**
     * @description 登录拦截放行时，设置虚拟角色
     */
    setVirtualRoles() {
    },
    /**
     * @description 设置token并发送提醒
     * @param {string} token 更新令牌
     * @param {string} tokenName 令牌名称
     */
    afterLogin(data: UserModuleType ) {
      const {token,username} = data
      const settingsStore = useSettingsStore()
      if (token) {
        this.setToken(token)
        const hour = new Date().getHours()
        const thisTime = hour < 8 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 18 ? '下午好' : '晚上好'
        gp.$baseNotify(`欢迎登录${settingsStore.title}`, `${thisTime}！`)
      } else {
        const err = `登录接口异常，未正确返回${tokenName}...`
        gp.$baseMessage(err, 'error', 'hey')
        throw err
      }


      // 如不使用username用户名,可删除以下代码
      if (username) this.setUsername(username)
    },
    /**
     * @description 登录
     * @param {*} userInfo
     */
    async login(userInfo: any) {
      const data = await AccountApi.login(userInfo)
      await this.afterLogin(data.data)
      await this.getMenuInfo()
    },

    async getMenuInfo() {

      const { routes, setRoutes } = useRoutesStore();
      await setRoutes();
    },
    /**
     * @description 获取用户信息接口 这个接口非常非常重要，如果没有明确底层前逻辑禁止修改此方法，错误的修改可能造成整个框架无法正常使用
     * @returns
     */
    async getUserInfo() {

    },
    /**
     * @description 退出登录
     */
    async logout() {
      await AccountApi.logout()
      await this.resetAll()
      await router.push('/login')
      //@ts-ignore
      // await location.reload(true)
    },
    /**
     * @description 重置token、roles、permission、router、tabsBar等
     */
    async resetAll() {
      const tabsStore = useTabsStore()
      await removeToken()
      this.token = null
      await tabsStore.delAllVisitedRoutes()
    },
  },
})
